package com.gqj.bas.controller;

import com.gqj.bas.domain.BasProvider;
import com.gqj.bas.service.IBasProviderService;
import com.gqj.common.annotation.Log;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.core.page.TableDataInfo;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 供应商信息Controller
 * 
 * @author lxq
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/bas/basProvider")
public class BasProviderController extends BaseController
{
    @Autowired
    private IBasProviderService basProviderService;

    /**
     * 查询供应商信息列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basProvider:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasProvider basProvider)
    {
        startPage();
        basProvider.setDelFlag("0");
        List<BasProvider> list = basProviderService.selectBasProviderList(basProvider);
        return getDataTable(list);
    }

    /**
     * 查询供应商列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basProvider:providerOptions')")
    @GetMapping("/providerOptions")
    public AjaxResult providerOptions(BasProvider basProvider)
    {
        List<Map<String, Object>> list = basProviderService.providerOptions(basProvider);
        return AjaxResult.success(list);
    }

    /**
     * 导出供应商信息列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basProvider:export')")
    @Log(title = "供应商信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasProvider basProvider)
    {
        List<BasProvider> list = basProviderService.selectBasProviderList(basProvider);
        ExcelUtil<BasProvider> util = new ExcelUtil<BasProvider>(BasProvider.class);
        util.exportExcel(response, list, "供应商信息数据");
    }

    /**
     * 获取供应商信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('bas:basProvider:query')")
    @GetMapping(value = "/{providerId}")
    public AjaxResult getInfo(@PathVariable("providerId") Long providerId)
    {
        return AjaxResult.success(basProviderService.selectBasProviderByProviderId(providerId));
    }

    /**
     * 新增供应商信息
     */
    @PreAuthorize("@ss.hasPermi('bas:basProvider:add')")
    @Log(title = "供应商信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasProvider basProvider)
    {
        basProvider.setCreateBy(getUsername());
        return toAjax(basProviderService.insertBasProvider(basProvider));
    }

    /**
     * 修改供应商信息
     */
    @PreAuthorize("@ss.hasPermi('bas:basProvider:edit')")
    @Log(title = "供应商信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasProvider basProvider)
    {
        basProvider.setUpdateBy(getUsername());
        return toAjax(basProviderService.updateBasProvider(basProvider));
    }

    /**
     * 删除供应商信息
     */
    @PreAuthorize("@ss.hasPermi('bas:basProvider:remove')")
    @Log(title = "供应商信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{providerIds}")
    public AjaxResult remove(@PathVariable Long[] providerIds)
    {
        for(Long providerId: providerIds){
            if (basProviderService.checkMaterialExistProvider(providerId))
            {
                return AjaxResult.error("供应商已被引用,不允许删除");
            }
        }
        return toAjax(basProviderService.deleteBasProviderByProviderIds(providerIds));
    }
}
